package com.inspur.project.system.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.inspur.framework.aspectj.lang.annotation.Log;
import com.inspur.framework.aspectj.lang.enums.BusinessType;
import com.inspur.project.system.domain.SysDistrict;
import com.inspur.project.system.service.ISysDistrictService;
import com.inspur.framework.web.controller.BaseController;
import com.inspur.framework.web.domain.AjaxResult;
import com.inspur.common.utils.poi.ExcelUtil;
import com.inspur.framework.web.page.TableDataInfo;

/**
 * 区域信息Controller
 *
 * @author inspur
 * @date 2020-03-14
 */
@RestController
@RequestMapping("/district/district")
public class SysDistrictController extends BaseController
{
    @Autowired
    private ISysDistrictService sysDistrictService;

    /**
     * 查询区域信息列表
     */
    @PreAuthorize("@ss.hasPermi('district:district:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysDistrict sysDistrict)
    {
//        startPage();
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        return getDataTable(list);
    }

    /**
     * 导出区域信息列表
     */
    @PreAuthorize("@ss.hasPermi('district:district:export')")
    @Log(title = "区域信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SysDistrict sysDistrict)
    {
        List<SysDistrict> list = sysDistrictService.selectSysDistrictList(sysDistrict);
        ExcelUtil<SysDistrict> util = new ExcelUtil<SysDistrict>(SysDistrict.class);
        return util.exportExcel(list, "district");
    }

    /**
     * 获取区域信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysDistrictService.selectSysDistrictById(id));
    }

    /**
     * 获取区域信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:query')")
    @GetMapping(value = "/code/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code)
    {
        return AjaxResult.success(sysDistrictService.selectSysDistrictByCode(code));
    }

    /**
     * 获取区域信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:query')")
    @GetMapping(value = "/parentId/{parentId}")
    public AjaxResult selectSysDistrictByParentId(@PathVariable("parentId") Long parentId)
    {
        return AjaxResult.success(sysDistrictService.selectSysDistrictByParentId(parentId));
    }

    /**
     * 获取区域信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:query')")
    @GetMapping(value = "/pcode/{pcode}")
    public AjaxResult selectSysDistrictByPCode(@PathVariable("pcode") String pcode)
    {
        return AjaxResult.success(sysDistrictService.selectSysDistrictByPCode(pcode));
    }

    /**
     * 新增区域信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:add')")
    @Log(title = "区域信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.insertSysDistrict(sysDistrict));
    }

    /**
     * 修改区域信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:edit')")
    @Log(title = "区域信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysDistrict sysDistrict)
    {
        return toAjax(sysDistrictService.updateSysDistrict(sysDistrict));
    }

    /**
     * 删除区域信息
     */
    @PreAuthorize("@ss.hasPermi('district:district:remove')")
    @Log(title = "区域信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable String[] codes) {
//        return toAjax(sysDistrictService.deleteSysDistrictByIds(codes));
        List<String> list = new ArrayList<>();
        for (String code : codes) {
            SysDistrict sysDistrict = new SysDistrict();
            sysDistrict.setPcode(code);
            int count = sysDistrictService.count(sysDistrict);
            if (count > 0) {
                list.add(code);
            }
        }
        if (list.size() > 0) {
            return AjaxResult.error(500, "区域编码为" + String.join("、", list) + "的节点存在子节点，请先删除子节点");
        } else {
            return toAjax(sysDistrictService.deleteSysDistrictByIds(codes));
        }
    }
}
